package Servlet;

import DAO.CustomerDAO;
import DAO.DAOFactory;
import DAO.StaffDAO;
import DAO.TechnicianDAO;
import javabean.Customer;
import javabean.Staff;
import javabean.Technician;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

@WebServlet("/registration")
public class RegistrationServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单数据
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String phone = req.getParameter("phone");
        String role = req.getParameter("role");//Customer admin staff technician
        PrintWriter out = resp.getWriter();
        resp.setContentType("text/html;charset=UTF-8");
        switch (role) {
            case "Customer":
                //封装成javabean
                Customer customer = new Customer();
                customer.setCustomerID(username);
                customer.setCustomerName(name);
                customer.setPassword(password);
                customer.setCustomerPhone(phone);
                //调用DAO层
                CustomerDAO customerDAO = (CustomerDAO) DAOFactory.getCustomerDAOInstance();
                try {
                    if (customerDAO.addCustomer(customer)) {
                        req.setAttribute("message", "true");
                        req.getRequestDispatcher("Login.jsp").forward(req, resp);
                    } else {
                        req.setAttribute("message", "false");
                        req.getRequestDispatcher("Registration.jsp").forward(req, resp);
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                break;
            case "staff":
                Staff staff = new Staff();
                staff.setStaffID(username);
                staff.setStaffName(name);
                staff.setPassword(password);
                staff.setStaffPhone(phone);
                StaffDAO staffDAO = (StaffDAO) DAOFactory.getStaffDAOInstance();
                if (staffDAO.addPendingStaff(staff)) {
                    req.setAttribute("message", "true");
                    req.getRequestDispatcher("Login.jsp").forward(req, resp);
                } else {
                    req.setAttribute("message", "false");
                    req.getRequestDispatcher("Registration.jsp").forward(req, resp);
                }
                break;
            case "technician":
                Technician technician = new Technician();
                technician.setTechnicianID(username);
                technician.setTechnicianName(name);
                technician.setPassword(password);
                technician.setTechnicianPhone(phone);
                TechnicianDAO technicianDAO = (TechnicianDAO) DAOFactory.getTechnicianDAOInstance();
                if (technicianDAO.addPendingTechnician(technician)) {
                    req.setAttribute("message", "true");
                    req.getRequestDispatcher("Login.jsp").forward(req, resp);
                } else {
                    req.setAttribute("message", "false");
                    req.getRequestDispatcher("Registration.jsp").forward(req, resp);
                }
                break;
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
